package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3TaskShortAdapter;
import com.das.mechanic_base.base.X3BaseDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class X3TaskShortDialog extends X3BaseDialog implements X3TaskShortAdapter.IOnClearStr, View.OnClickListener, X3TaskInputShortWindow.IOnGetTaskShort {

    ImageView iv_add;
    RecyclerView rlv_word;
    private X3TaskShortAdapter shortAdapter;
    private X3BottomTaskShortDialog shortWindow;

    public X3TaskShortDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_add_task_short;
    }

    @Override
    protected void initView() {
        iv_add = getView(R.id.iv_add);
        rlv_word = getView(R.id.rlv_word);

        rlv_word.setLayoutManager(new LinearLayoutManager(mContext));
        shortAdapter = new X3TaskShortAdapter(mContext);
        rlv_word.setAdapter(shortAdapter);
        shortAdapter.setiOnClearStr(this);
        iv_add.setOnClickListener(this);
    }

    @Override
    public void show() {
        super.show();
        requestListShort();
    }

    @Override
    protected float getWindowWidth() {
        return 0.9f;
    }

    @Override
    public void iOnClearShortStr(String content) {
        delShortStr(content);
    }

    @Override
    public void iOnSelectShortStr(String content) {
        dismiss();
        if (iOnShowSelect != null) {
            iOnShowSelect.iOnSelectTaskContent(content);
        }
    }

    @Override
    public void iOnAddShortStr() {
        iv_add.performClick();
    }

    /**
     * 删除快捷短语
     * @param content
     */
    private void delShortStr(String content) {
        NetWorkHttp.getApi().delMyShortStr(content)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        requestListShort();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_add) {
            if (shortWindow == null) {
                shortWindow = new X3BottomTaskShortDialog(mContext);
                shortWindow.setiOnGetTaskShort(X3TaskShortDialog.this);
            }
            shortWindow.show();
        }
    }

    @Override
    public void iOnGetTaskShort(String content) {
        NetWorkHttp.getApi().saveMyShortStr(content)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        requestListShort();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    /**
     * 请求快捷短语
     */
    private void requestListShort() {
        NetWorkHttp.getApi().listMyShortStr()
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<List<String>>() {
                    @Override
                    protected void onDone(List<String> mList) {
                        if (X3StringUtils.isListEmpty(mList)) {
                            mList = new ArrayList<>();
                        }
                        if (shortAdapter != null) {
                            shortAdapter.changeShort(mList);
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    IOnShowSelect iOnShowSelect;

    public void setiOnShowSelect(IOnShowSelect iOnShowSelect) {
        this.iOnShowSelect = iOnShowSelect;
    }

    public interface IOnShowSelect {
        void iOnSelectTaskContent(String content);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("待办事项添加快捷短语弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("待办事项添加快捷短语弹窗");
    }
}
